package com.kx.yooli.kx.wechat.config.propertiesconf;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @author john_sqwen-childe.wen
 * date 2024/4/1
 * PROJECT_NAME wechat
 * description create CustomizedPropertyConfigurer by john_sqwen
 * and  value is
 */
@Component
public class CustomizedPropertyConfigurer {
    private static final Logger log = LoggerFactory.getLogger(CustomizedPropertyConfigurer.class);

    /**
     * @param not param by this function it's loading resource dir
     * @return PropertySourcesPlaceholderConfigurer
     * description this  is properties read values
     */
   private static Properties getPropertiesConfigurer(){
     Properties pros = new Properties() ;
       InputStreamReader inputStreamReader = null;
       try {
           inputStreamReader = new InputStreamReader(Objects.requireNonNull(CustomizedPropertyConfigurer.class.getClassLoader().getResourceAsStream("application-basic.properties")), StandardCharsets.UTF_8);
           pros.load(inputStreamReader);
       } catch (IOException e) {
           log.info("loading properties if fail message is :{}",e.getMessage());
           throw new RuntimeException(e);
       }catch (NullPointerException ex){
           log.info("read properties file is fail message is :{}",ex.getMessage()+ex.fillInStackTrace());
           throw  new RuntimeException(ex);
       }
       return  pros;
   }
  public static String getContextProperty(String propName){
      Properties propertiesConfigurer = getPropertiesConfigurer();
      return propertiesConfigurer.getProperty(propName);
  }
  private static  Properties getMailPropertiesConfigurer(){
      Properties pros = new Properties() ;
      InputStreamReader inputStreamReader = null;
      try {
          inputStreamReader = new InputStreamReader(Objects.requireNonNull(CustomizedPropertyConfigurer.class.getClassLoader().getResourceAsStream("application-mail.properties")), StandardCharsets.UTF_8);
          pros.load(inputStreamReader);
      } catch (IOException e) {
          log.info("loading properties if fail message is :{}",e.getMessage());
          throw new RuntimeException(e);
      }catch (NullPointerException ex){
          log.info("read properties file is fail message is :{}",ex.getMessage()+ex.fillInStackTrace());
          throw  new RuntimeException(ex);
      }
      return  pros;
  }
    public static Properties getMailContextProperty(){
        return getMailPropertiesConfigurer();
    }
}
